package vampirism.core.client;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ButtonGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;


public class MenuCreate {
	
    public Window window;
    public final Button createButton;
    public String[] playerDescs = {"Warlock specializes in dark magic and controlling the undead.",
    							 "Warrior is a strong melee fighter capable of using a shield.",
    							 "Rogue can move faster and stealther than any other class.",
    							 "Priest is able to heal himself and his allies.",
    							 "Mage can cast various magic spells.",
    							 "Archer uses his precision to strike down opponents from afar."};
    public Button[] playerButtons;   
    
    public ButtonGroup buttonGroup = new ButtonGroup();
    
    public MenuCreate(final MainGame main){
    	
    	System.out.println("#CLIENT: showing create interface");
    	
    	buttonGroup.setMinCheckCount(1);
    	buttonGroup.setMaxCheckCount(1);
    	
	    createButton = new TextButton("Create", main.skin);
	    
	    playerButtons = new Button[main.playerTypeNames.length];
	    
	    for(int i = 0; i < 5; i++){
		    Image img = new Image(main.framesPlayer[21*i]);
		    img.setScale(4);
		    img.setOriginX(4);
		    img.setOriginY(4);
		    playerButtons[i] = new Button(img, main.skin, "toggle");
	    }
	    
	    final Label titleLabel1 = new Label("Create your new character.", main.skin);
	
	    final Label nameLabel = new Label("Name: ", main.skin);
	    final TextField nameTextField = new TextField("test", main.skin);
	
	    window = new Window("", main.skin);
	    window.setMovable(false);
	    window.defaults().spaceRight(20);
	    window.defaults().spaceLeft(20);
	    window.defaults().spaceTop(20);
	    window.defaults().spaceBottom(20);
	    window.row().fill().expandX();
	    window.row();
	    window.add(titleLabel1).colspan(5).padTop(10);
	    window.row();
	    window.add(nameLabel).padLeft(10);
	    window.add(nameTextField).width(200);
	    window.row();
	    
	    for(int i = 0; i < playerButtons.length; i++){
	    	buttonGroup.add(playerButtons[i]);
	    	window.add(playerButtons[i]).padTop(-20).minWidth(40).minHeight(40);
	    	window.row();
	    }
	    window.add(createButton).colspan(5).padBottom(10);
	    window.pack();
	    
	    window.setPosition((main.W-window.getWidth())/2.0f, (main.H-window.getHeight())/2.0f);

	    createButton.addListener(new ChangeListener() {
	            public void changed(ChangeEvent event, Actor actor) {
	            	byte typeChosen = -1;
	            	for(byte i = 0; i < playerButtons.length; i++){
	            		if(playerButtons[i].isChecked()){
	            			typeChosen = i;
	            			System.out.println("CHECKED: "+i);
	            			break;
	            		}
	            	}
	            	createButton.setDisabled(true);
	            	main.clientSide.simulateCreatePlayer(nameTextField.getText(), typeChosen);
	            }
		});
	   
	    
	    
    }
}
